package com.minew.beaconset;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.minew.beaconset.beaconset.BeaconsetManager;
import com.minew.beaconset.bean.LocaltionLog;
import com.minew.beaconset.keeplive.KeepLive;
import com.minew.beaconset.permission.Permission;
import com.minew.beaconset.permission.PermissionManager;

import java.util.List;

public class MainActivity extends Activity {

    /**
     * 需要的权限
     */
    private String[] permission = {
            Permission.WRITE_EXTERNAL_STORAGE,
            Permission.READ_EXTERNAL_STORAGE,
            Permission.ACCESS_COARSE_LOCATION
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        data = findViewById(R.id.data);

        PermissionManager.get(this).check(permission
        , () -> {
                    KeepLive.initKeepLive();
                    BeaconsetManager.getInstance().setRssiRange(-81,-78);
                    getData();
        });
    }


    public void getData(View view) {
        getData();
    }
    private TextView data;
    private void getData(){
        BeaconsetManager.getInstance().getLocationData(map -> {
            String txt = "";
            for(String key:map.keySet()){
                List<LocaltionLog> logs = map.get(key);
                for(LocaltionLog log:logs){
                    txt+=log.toString();
                }
            }
            String finalTxt = txt;
            runOnUiThread(() -> data.setText(finalTxt));

        });
        new Handler().postDelayed(() -> getData(),3000);
    }

    public void cleanData(View view) {
        BeaconsetManager.getInstance().cleanData(() -> {
            getData();
        });
    }
}
